/*
 *  map view with  buttons
 */
package com.andrawid;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import java.util.List;

/**
 *
 * @author Cx
 */
public class GmapActivity extends MapActivity implements LocationListener {

    public final static int DEFAULT_LAT = 40450000;
    public final static int DEFAULT_LON = -79950000;
    int lat = DEFAULT_LAT;
    int lon = DEFAULT_LON;
    MapView mapView;
    GeoPoint p;
    MapController mController;
    MyLocationOverlay myLocationOverlay;
    private LocationManager locationManager;

//    private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.gmap);

        initMap();

        initLocation();

        //   mapView.invalidate(); //?
    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }

    @Override
    public void onResume() {
        super.onResume();
  //      locationManager.requestLocationUpdates(
  //              LocationManager.GPS_PROVIDER, 5000L, 50.0f, this);
    }

    @Override
    public void onPause() {
        super.onPause();
      //  locationManager.removeUpdates(this);

    }

    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void onProviderEnabled(String arg0) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void onProviderDisabled(String arg0) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    private void initMap() {
        mapView = (MapView) findViewById(R.id.mapView);
        mapView.displayZoomControls(true); //why does this not show up?
        mapView.setStreetView(false);

        mController = mapView.getController();

        p = new GeoPoint(lat, lon);
        mController.animateTo(p);
        mController.setZoom(16);

        mapView.setBuiltInZoomControls(true); //turns on the standard zoom controls
    }

    private void initLocation() {

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        //get locations every 5000 ms or when the user moves more than 50 m.
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000L, 50.0f, this);

        addDataPoint();
    }

    public void onLocationChanged(Location location) {
        if (location != null) {
//update the location and display it on the map
            List overlays = mapView.getOverlays();
            myLocationOverlay = new MyLocationOverlay(this, mapView);
            overlays.add(myLocationOverlay);
            myLocationOverlay.enableMyLocation();

            addDataPoint();

            drawData();
        }
    }

    private void addDataPoint() {
        //TODO add point to arraylist
    }

    private void drawData() {
        //go through datalist

        //refer to support09_googgle_maps
    }
}
